<?php

namespace App;

use App\Http\Services\FileManager;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * App\Videos
 *
 * @property int $id
 * @property int $file_id 文件id
 * @property string|null $key
 * @property string|null $video_name 视频名称
 * @property string|null $video_type 视频类型
 * @property string|null $deleted_at
 * @property \Carbon\Carbon|null $created_at
 * @property \Carbon\Carbon|null $updated_at
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Query\Builder|\App\Videos onlyTrashed()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Videos whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Videos whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Videos whereFileId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Videos whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Videos whereKey($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Videos whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Videos whereVideoName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Videos whereVideoType($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Videos withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\Videos withoutTrashed()
 * @mixin \Eloquent
 * @property-read \App\Files $files
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Videos newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Videos newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Videos query()
 */
class Videos extends Model
{
    protected $table = "videos";
    use SoftDeletes;

    public function files(){
        return $this->belongsTo('App\Files','file_id');
    }

    /**
     * 获取视频名称与路径
     * @param $video_id
     * @return array
     */
    public static function getNameAndUrl($video_id){
        $data=[
            'name'=>'',
            'url'=>''
        ];
        $record=self::whereId($video_id)->first();
        if ($record){
            $file=Files::whereId($record->file_id)->first();
            if (!$file)
                return $data;
            $data['name']   =$file ? $file->file_name : '';

            if($file->disk == "oss"){
                $data['url'] = env('OSS_FILE_URL').'/'.$file->file_url.'/'.$file->file_md5.$file->file_type;
            } elseif($file->disk == "local"){
                $data['url'] =route("Api.File.showVideoById",["id"=>$record->id]);
            }

        }
        return $data;
    }
}
